package ldh.fx.adminfx.util;

import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.PieChart;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Label;
import javafx.scene.control.Tooltip;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.stage.Popup;

import java.util.Random;

public class ChartUtil {

    public static void showPopupInfo(PieChart pieChart) {
        final Label caption = new Label("");
        caption.setTextFill(Color.DARKORANGE);
        caption.setStyle("-fx-font: 24 arial;");

        Popup popup = new Popup();
        popup.setAutoHide(true);
        popup.getContent().add(caption);

        for (final PieChart.Data data : pieChart.getData()) {
            data.getNode().addEventHandler(MouseEvent.MOUSE_PRESSED, e->{
                caption.setText(String.valueOf(data.getPieValue()) + "%");
                popup.show(pieChart.getScene().getWindow(), e.getScreenX(), e.getScreenY());
            });
        }
    }

    public static void showPopupInfo(LineChart<?, ?> lineChart) {
        final Label caption = new Label("");
        caption.setTextFill(Color.DARKORANGE);
        caption.setStyle("-fx-font: 24 arial;");

        Popup popup = new Popup();
        popup.setAutoHide(true);
        popup.getContent().add(caption);

        for(XYChart.Series<?, ?> series : lineChart.getData()) {
            for (XYChart.Data entry : series.getData()) {

                entry.getNode().addEventHandler(MouseEvent.MOUSE_PRESSED, e->{
                    caption.setText(entry.getXValue() + ", " + entry.getYValue());
                    popup.show(lineChart.getScene().getWindow(), e.getScreenX(), e.getScreenY());
                });
            }
        }
    }

    private static final Random random = new Random();
    public static int random(int start, int end) {
        int v = random.nextInt(end);
        while(v < start) {
            v = random.nextInt(end);
        }
        return v;
    }

    public static double randomDouble(double start, double end) {
        double v = random.nextDouble() * end;
        while(v < start) {
            v = random.nextDouble() * end;
        }
        return v;
    }
}
